<template>
  <div>
    <el-dialog
      :title="info.type == 'add' ? '新增菜单' : '编辑菜单'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="角色名称" prop="rolename">
            <el-input v-model="ruleForm.rolename"></el-input>
          </el-form-item>
          <el-form-item label="权限" prop="title">
            <!-- node-key 选择节点的方式 -->
            <!-- highlight-current 高亮显示当前项 -->
            <el-tree
              :data="data"
              show-checkbox
              default-expand-all
              node-key="id"
              ref="tree"
              highlight-current
              :props="defaultProps"
            >
            </el-tree>
          </el-form-item>
          <el-form-item label="状态" prop="menus">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.isShow = false">取 消</el-button>
        <el-button type="primary" @click="info.isShow = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { roleAdd,roleEdit } from "@/api.js";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        rolename: "",
        menus: "",
        status: 1,
      },
      rules: {
        rolename: [{ required: true, message: "请输入菜单标题", trigger: "blur" }],
      },
      data: [],
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  created() {
    this.$http
      .get("/menulist?istree=1")
      .then((res) => {
        // console.log(res);
        this.data = res.list;
        // console.log(this.menulist);
      })
      .catch((err) => {
        console.log(err);
      });
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // getCheckedKeys: 是el-trew这个组件自带的函数,可以获取被勾选的节点,组成一个数组然后返回
          this.ruleForm.menus=this.$refs.tree.getCheckedKeys().join(',')
          if (this.info.type === "add") {
            console.log(this.ruleForm);
            return
            // let res=await roleAdd(this.ruleForm);
          } else {
            let res = await roleEdit(this.ruleForm);
          }
          this.info.isShow = false;
          this.$emit("update");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //给表单复制
    setValue(info) {
      this.ruleForm = { ...info };
      let arr=this.ruleForm.menus.split(',')
      //setCheckedKeys是el-tree自带的函数，功能是根据数组勾选节点
      //Vue在更新DOM的时候是异步执行的，所以执行到这一句时，el-tree还没有生成，所以会报错
      //setCheckedKeys执行的晚一些就不报错了，DOM更新完成完成后被调用Vue.nextTick(callback)中的回调函数。
      this.$nextTick(()=>{
        this.$refs.tree.setCheckedKeys(arr)
      })
    },
  },
};
</script>
<style scoped>
</style>